#include "m_housemastermanage.h"
#include "ui_m_housemastermanage.h"
#include "m_hminsert.h"
#include "m_hmupdate.h"

QString global_id2;
M_houseMasterManage::M_houseMasterManage(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::M_houseMasterManage)
{
    ui->setupUi(this);
    querytable();
}

M_houseMasterManage::~M_houseMasterManage()
{
    delete ui;
}
void  M_houseMasterManage::querytable()
{
    QString str=QString("select * from housemaster left outer join hb  on(housemaster.hid=hb.hou_hid)");
    qDebug()<<str;
    model.setQuery(str);
    ui->tableView->setModel(&model);
    ui->tableView->setColumnHidden(7,true);
    model.setHeaderData(0,Qt::Horizontal,"工号");
    model.setHeaderData(1,Qt::Horizontal,"姓名");
    model.setHeaderData(2,Qt::Horizontal,"性别");
    model.setHeaderData(3,Qt::Horizontal,"电话");
    model.setHeaderData(4,Qt::Horizontal,"住址");
    model.setHeaderData(5,Qt::Horizontal,"密码");
    model.setHeaderData(6,Qt::Horizontal,"管理楼栋");
    model.setHeaderData(8,Qt::Horizontal,"工作时间");

}
void M_houseMasterManage::on_insertBtn_clicked()
{
    m_HMinsert window;
    window.exec();
    querytable();
}


void M_houseMasterManage::on_deleteBtn_clicked()
{
    int row = ui-> tableView ->currentIndex().row();
    QString id=model.data(model.index(row,0)).toString();

    QSqlQuery query;
    QString str=QString("delete from housemaster where hid='%1'").arg(id);
    if(query.exec(str)){
        QMessageBox::information(NULL,"tips","删除成功");
        querytable();
    }
    else {
        QMessageBox::warning(NULL,"Error","删除失败");
    }
    querytable();
}


void M_houseMasterManage::on_updateBtn_clicked()
{
    int row = ui-> tableView ->currentIndex().row();
    QString id=model.data(model.index(row,0)).toString();

    global_id2=id;
    m_HMupdate window;
    window.exec();
    querytable();
}

